package beautiful.butterfly.schedule;

import beautiful.butterfly.schedule.strategy.ScheduleFactory;
import beautiful.butterfly.schedule.task_manager.IDataManager;
import beautiful.butterfly.schedule.zookeeper.Manager;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

@Slf4j
public class ScheduleContext {
    //bug
    public final static String configFile = System.getProperty("user.dir") + File.separator + "pamirsScheduleConfig.properties";
    //protected static transient Logger log = LoggerFactory.getLogger(ScheduleContext.class);
    private static ScheduleFactory scheduleFactory;

    public static boolean isInitial() throws Exception {
        return scheduleFactory != null;
    }

    public static boolean initial() throws Exception {
        if (scheduleFactory != null) {
            return true;
        }
        File file = new File(configFile);
        scheduleFactory = new ScheduleFactory();
        scheduleFactory.start = false;

        if (file.exists() == true) {
            //Console��������������
            Properties properties = new Properties();
            FileReader reader = new FileReader(file);
            properties.load(reader);
            reader.close();
            scheduleFactory.init(properties);
            log.info("����Schedule�����ļ���" + configFile);
            return true;
        } else {
            return false;
        }
    }

    public static ScheduleFactory getScheduleFactory() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return scheduleFactory;
    }

    public static void setScheduleFactory(ScheduleFactory scheduleFactory) {
        ScheduleContext.scheduleFactory = scheduleFactory;
    }

    public static IDataManager getScheduleDataManager() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return scheduleFactory.getDataManager();
    }

    public static Manager getStrategyDataManagerWithZooKeeperManager() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return scheduleFactory.getManager();
    }

    public static Properties load() throws IOException {
        File file = new File(configFile);
        if (!file.exists()) {
            System.out.println("configFile:" + configFile);
            throw new NullPointerException();
        } else {
            Properties properties = new Properties();
            FileReader fileReader = new FileReader(file);
            properties.load(fileReader);
            fileReader.close();
            return properties;
        }

    }

    public static void saveConfigInfo(Properties properties) throws Exception {
        try {
            FileWriter fileWriter = new FileWriter(configFile);
            properties.store(fileWriter, "");
            fileWriter.close();
        } catch (Exception ex) {
            throw new Exception("����д��������Ϣ���ļ���" + configFile, ex);
        }
        if (scheduleFactory == null) {
            initial();
        } else {
            scheduleFactory.reInit(properties);
        }
    }

}
